import { BasicDrawer, useDrawerInner } from "@/components/Drawer";
import { BasicForm, FormSchema, useForm } from "@/components/Form";
import { PurPlanWhsApi } from "@/api/erp/purchase/purPlanWhs";
import { message } from "antd";
import { useTranslation } from "react-i18next";
import { useState, useMemo } from "react";
import { productApi } from "@/api/erp/drug/drug";
import { SupplierApi } from "@/api/erp/purchase/supplier";
import { SoaApi } from "@/api/erp/property/soa";
import { WarehouseApi } from "@/api/erp/stock/warehouse";
let reload: Function;
let id: any;
const index = (props: { register: Fn }) => {
	const [isUpdated, setIsUpdated] = useState(false);
	const { t } = useTranslation();
	const en_vi = "drugStorage.purchasingManagement.other";
	const formSchema: FormSchema[] = [
		{
			label: t("drugStorage.inventoryManagement.drugInventory.warehouseName"),
			field: "whsId",
			component: "ApiSelect",
			componentProps: {
				api: WarehouseApi.getWarehouseSimpleList1,
				labelField: "whsName",
				valueField: "whsId"
			}
		},
		{
			label: t("drugStorage.productName"),
			component: "ApiSelect",
			field: "drugId",
			componentProps: {
				api: productApi.getproductSimpleList,
				labelField: "productName",
				valueField: "productId",
				immediate: false,
				params: {
					bizType: "1"
				}
			}
		},
		{
			label: t(`${en_vi}.supler`),
			component: "ApiSelect",
			field: "suplerId",
			componentProps: {
				api: SupplierApi.getSupplierSimpleList,
				labelField: "name",
				valueField: "suplerId",
				immediate: false
			}
		},
		{
			label: t(`${en_vi}.fundingId`),
			component: "ApiSelect",
			field: "fundingId",
			componentProps: {
				api: SoaApi.getSoaPage,
				labelField: "fundingName",
				valueField: "fundingId",
				immediate: false
			}
		},
		{
			label: t(`${en_vi}.planDate`),
			field: "planDate",
			component: "DatePicker",
			componentProps: {
				picker: "year"
			}
		},
		{
			label: t(`${en_vi}.unit`),
			field: "unit",
			component: "Input"
		},
		{
			label: t(`${en_vi}.unitPrice`),
			field: "unitPrice",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.qtyIn`),
			field: "qtyIn",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.qtyOut`),
			field: "qtyOut",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.stockQty`),
			field: "stockQty",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.counterQty`),
			field: "counterQty",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.total`),
			field: "total",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.level1`),
			field: "level1",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.level2`),
			field: "level2",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.level3`),
			field: "level3",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.level4`),
			field: "level4",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.price`),
			field: "price",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.number`),
			field: "number",
			component: "InputNumber"
		},
		{
			label: t(`${en_vi}.execMeth`),
			field: "execMeth",
			component: "Input"
		}
	];
	const getTitle = useMemo(() => {
		return isUpdated ? t("felix_ui.edit") : t("felix_ui.add");
	}, [isUpdated]);
	const [register, { validateFields, setFieldsValue, resetFields }] = useForm({
		schemas: formSchema,
		labelWidth: 150,
		baseColProps: {
			span: 12
		}
	});
	const [registerDrawer, { setDrawerProps, closeDrawer }] = useDrawerInner(props.register, data => {
		reload = data.reload;
		id = data.record?.id;
		resetFields();
		setDrawerProps({ confirmLoading: true });
		setIsUpdated(!!data?.isUpdated);
		setFieldsValue({
			...data.record
		});
		setDrawerProps({ confirmLoading: false });
	});
	const handleSubmit = async () => {
		const formValue = await validateFields();
		formValue.planDate = formValue.planDate?.substring(0, 4);
		let res;
		if (isUpdated) {
			formValue.id = id;
			res = await PurPlanWhsApi.updatePurPlanWhs(formValue);
		} else {
			res = await PurPlanWhsApi.createPurPlanWhs(formValue);
		}
		if (!res) {
			message.error(t("viewComponents.patientInformation.additionFailed"));
			return;
		}
		message.success(t("viewComponents.patientInformation.successfullyAdded"));
		closeDrawer();
		reload();
	};
	return (
		<BasicDrawer size="large" title={getTitle} showFooter={true} {...props} register={registerDrawer} ok={handleSubmit}>
			<BasicForm register={register} labelWrap={true}></BasicForm>
		</BasicDrawer>
	);
};
export default index;
